<?
Class newsModule{
    var $_connect;
    var $error;
    function newsModule() {
        $this->_connect = new Dataconnection();
    }
    function geterror(){
        return $this->error;
    }
    function getNew($id){
        $SQLCommand="
        SELECT
            `news`.*,
            `categorys`.`name` as categoryname
        FROM
            `news`
            LEFT JOIN `categorys` ON `news`.`category` =`categorys`.`id`
            
        WHERE
            `news`.`id`='$id'
        ";
        $data=$this->_connect->executeSQL($SQLCommand);
        $this->error.=$this->_connect->error;
        $result=array();
        if($data!=null ){
            $result=$data->fetch_array();
            return $result;
        }
        return null;
    }
    
    function getNews($p,$n,$q,$category){
        $str=($p-1)*$n;
        $SQLCommand="
        SELECT
            `news`.*,
            `categorys`.`name` as categoryname,
            `categorys`.`note` as categorynote
        FROM
            `news`
            LEFT JOIN `categorys` ON `news`.`category` =`categorys`.`id`
        WHERE
            `news`.`category`='$category'
        ORDER BY `insert` DESC
        LIMIT $str,$n
            
        ";
        
        $data=$this->_connect->executeSQL($SQLCommand);
        $this->error.=$this->_connect->error;
        $result=array();
        if($data!=null ){
            while ($row=$data->fetch_array())
                array_push($result, $row);
            return $result;
        }
        return null;
    }
    function getNew_NumRow($q,$category){
        
        $SQLCommand="
        SELECT
            count(*) as numrow
        FROM
            `news`
            
        WHERE
            `news`.`category`='$category'
        ";
        $data=$this->_connect->executeSQL($SQLCommand);
        $this->error.=$this->_connect->error;
        if($data!=null ){
            $row=$data->fetch_array();
            return $row["numrow"];
        }
        return 0;
    }
    function insertNew($category,$image,$smaill_image,$title,$second_title, $brief, $content,$img1,$img2,$img3,$img4,$img5,$album){
        
        $SQLCommand="
        INSERT 
            `news`(
                `category`,`image`,`small_image`,`title`,`title_second`,`brief`,`content`,`insert`,`img1`,`img2`,`img3`,`img4`,`img5`,`album`
            )
            VALUE(
                '$category','$image','$smaill_image','$title','$second_title','$brief','$content',now(),'$img1','$img2','$img3','$img4','$img5',$album
            )
        ";
        $n=$this->_connect->executeNoneSQL($SQLCommand);
        $this->error.=$this->_connect->error;
        if($n==1) {
            return true;
        }
        return false;
    }
    function updateNew($id,$category,$image,$smaill_image,$title,$second_title, $brief, $content,$img1,$img2,$img3,$img4,$img5,$album){
        $SQLCommand="
        UPDATE 
            `news`
        SET `category`='$category',
            `image`='$image',
            `title`='$title',
            `small_image`='$smaill_image',
            `title_second`='$second_title',
            `brief`='$brief',
            `content`='$content',
            `img1`='$img1',
            `img2`='$img2',
            `img3`='$img3',
            `img4`='$img4',
            `img5`='$img5',
            `album`=$album,
            `update`=now()
        WHERE `id`='$id'
        ";
        $n=$this->_connect->executeNoneSQL($SQLCommand);
        $this->error.=$this->_connect->error;
        if($n==1) {
            return true;
        }
        return false;
    }
    function deleteNew($id){
        $SQLCommand="
        UPDATE 
            `news`
        SET `delete`=now()
        WHERE `id`='$id'
        ";
        $n=$this->_connect->executeNoneSQL($SQLCommand);
        $this->error.=$this->_connect->error;
        if($n==1) {
            return true;
        }
        return false;
    }
    function restoreNew($id){
        $SQLCommand="
        UPDATE 
            `news`
        SET `delete`=null
        WHERE `id`='$id'
        ";
        $n=$this->_connect->executeNoneSQL($SQLCommand);
        $this->error.=$this->_connect->error;
        if($n==1) {
            return true;
        }
        return false;
    }
}
?>